package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.util.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
public class MobileValidateCodeController {

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 发送验证码
     *
     * @return
     */
    @PostMapping("/send")
    public Result send(String type, String telephone) {
        log.info("验证码类型:{}，手机号:{}", type, telephone);
        //生成4位随机验证码
        Integer code = ValidateCodeUtils.generateValidateCode(4);
        System.out.println(type + "验证码：" + code);
        //将验证码存入Redis中,并设置有效期为2分钟
        redisTemplate.opsForValue().set(telephone+"_"+type, code, 2, TimeUnit.MINUTES);
        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS, null);

    }

}
